var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page: 1,
        listRows: 10,
        total: 0,
        order:'',
        divisionEdit:'',
        groupId:''
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page: _this.params.page,
            listRows: _this.params.listRows,
            order:_this.params.order,
        };
        Api.admingrouplist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPages;
            layui.use('laypage', function() {
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'page',
                    index: 1,
                    theme: '#3498db',
                    curr: _this.params.page,
                    count: _this.params.total, //数据总数，从服务端得到
                    hash: 'curr_page',
                    layout: ['prev', 'page', 'next', 'skip'],
                    jump: function(obj, first) {
                        _this.params.page = obj.curr;
                        //obj包含了当前分页的所有参数，比如：curr当前页
                        var Params = {
                            passport: _this.params.passport,
                            page: _this.params.page,
                            listRows: _this.params.listRows,
                            order:_this.params.order,
                        };
                        Api.admingrouplist(Params, function(data) {
                            _this.getDataList(data.list);

                        })
                    }
                });
            });
        });
    },
    getDataList: function(data) {
        var _this = this;

        if (data && data.length > 0) {
            var html = '',
                i = 0,
                len = data.length;
            for (i; i < len; i++) {
                html += '<tr>'+
                            '<td class="text-c">'+data[i].roleName+'</td>'+
                            '<td class="text-c">'+data[i].functionDescription+'</td>'+
                            '<td class="text-c">'+data[i].adminNum+'</td>'+
                            '<td class="text-c">'+data[i].gmtCreate+'</td>';

                    if (data[i].isUse==1) {
                        html+='<td class="text-c">'+
                                    '<span groupId="'+data[i].groupId+'" class="switch switch-on new" themeColor="#3498db"></span>'+
                                '</td>';
                    }else{
                        html+='<td class="text-c">'+
                                    '<span groupId="'+data[i].groupId+'" class="switch switch-off new" themeColor="#3498db"></span>'+
                                '</td>';
                    };
                    html+=
                        '<td class="text-c">'+
                            '<a class="handle manageSet" groupId="'+data[i].groupId+'">权限设置</a><a class="handle edit" groupId="'+data[i].groupId+'" >编辑</a><a groupId="'+data[i].groupId+'" class="handle delete">删除</a>'+
                        '</td>'+
                    '</tr>';
            }
            $('#List').html(html);
            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        }
        else {
            var html = '<tr>' +
                '<td class="text-c c-666" colspan="6" >暂无数据</td>' +
                '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent:function(){
        var _this = this;
        honeySwitch.init();
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.edit').on('click', function() {
            var groupId=$(this).attr('groupId');
            _this.params.divisionEdit=1;
            $('#modal_search').modal('show');
            _this.divisionDetail(groupId);
        });

        $('.manageSet').on('click', function() {
            Pub.setSession('groupId', $(this).attr('groupId'));
            window.location.href = Pub.getHtmlUrl('./manage-division-set.html');
        });
        $('.switch').on('click',function(event) {
            event.preventDefault();
            var groupId=$(this).attr('groupId');
            var datas=null;
            if ($(this).hasClass('switch-on')) {
                datas=1;
            }else{
                datas=0;
            };
            _this.changeOnline(groupId,datas);
        });
        $('.delete').click(function(event) {
            var id=$(this).attr('groupId');
            var _self=this;
            layui.use('layer', function(){
                  layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                  }, function(index){
                        _this.deleteAd(id);
                        layer.close(index);
                  }, function(){

                  });
            });
        });
    },
    divisionDetail:function(groupId){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            groupId:groupId,
        };
        Api.admingrouplistdetails(paramsObj, function(data){
            console.log(data);
            $('#roleName').val(data.roleName);
            $('#functionDescription').val(data.functionDescription);
            _this.params.groupId=data.groupId;
        });
    },
    changeOnline:function(groupId,isUse){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            groupId:groupId,
            isUse:isUse,
        };
        Api.admingroupisuse(paramsObj, function(data){
            console.log(data);

        });
    },
    deleteAd:function(id){
        var _this = this;
        console.log(id);
        var paramsObj = {
            passport: _this.params.passport,
            groupId:id
        };
        Api.admingroupdelete(paramsObj, function(data){
            console.log(data);
            layui.use('layer', function(){
                layer.msg('删除成功', {icon: 6});
            });
            _this.params.page=1;
            _this.getData();

        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('#addDivision').click(function(event) {
            $('#roleName').val('');
            $('#functionDescription').val('');
            $('#modal_search').modal('show');
            _this.params.divisionEdit=0;
        });
        $('#divisionYes').click(function(event) {
            var roleName=$('#roleName').val();
            var functionDescription=$('#functionDescription').val();
            if (_this.params.divisionEdit==1) {
                _this.admingroupmodify(roleName,functionDescription);

            }else{
                _this.admingroupadd(roleName,functionDescription);

            };
        });
    },
    admingroupmodify:function(roleName,functionDescription){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            groupId:_this.params.groupId,
            roleName:roleName,
            functionDescription:functionDescription
        };
        Api.admingroupmodify(paramsObj, function(data){
            console.log(data);
            _this.getData();
            $('#modal_search').modal('hide');
        });
    },
    admingroupadd:function(roleName,functionDescription){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            roleName:roleName,
            functionDescription:functionDescription
        };
        Api.admingroupadd(paramsObj, function(data){
            console.log(data);
            _this.getData();
            $('#modal_search').modal('hide');
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();


    }
};
var page = new Page();